﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data;

namespace WCFGPSTrackingApp
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "WCFServiceApp" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select WCFServiceApp.svc or WCFServiceApp.svc.cs at the Solution Explorer and start debugging.
    public class WCFServiceApp : IWCFServiceApp
    {

        public List<Entities.GPSImeiInfo> GetGPSImeiInfo(int companyID, string username, bool isUser, bool isAdmin, bool isMaster){
                        
            //Entities.GPSImeiInfo GPSImeiInfoReturn = new Entities.GPSImeiInfo();
            List<Entities.GPSImeiInfo> GPSImeiInfoReturn = new List<Entities.GPSImeiInfo>();
            Entities.GPSImeiInfo GPSImeiInfo;

            try
            {
                DataSet ds = GPSTrackingDBV2Custom.Data.DataRepository.TrackDataLastLocationProvider.GetGroupsTrackDataLastLocationByUser(companyID, username, isUser, isAdmin, isMaster);

                int total = 0;
                if (ds.Tables.Count > 0)
                {
                    total = ds.Tables[0].Rows.Count;

                    for (int i = 0; i < total; i++)
                    {
                        GPSImeiInfo = new Entities.GPSImeiInfo();

                        GPSImeiInfo.GPSGroupID = int.Parse(ds.Tables[0].Rows[i]["GPSGroupID"].ToString());
                        GPSImeiInfo.GPSGroupName = ds.Tables[0].Rows[i]["GPSGroupName"].ToString();
                        GPSImeiInfo.GPSImei = ds.Tables[0].Rows[i]["GPSImei"].ToString();
                        GPSImeiInfo.GPSImeiDescription = ds.Tables[0].Rows[i]["GPSImeiDescription"].ToString();
                        GPSImeiInfo.GPSImeiName = ds.Tables[0].Rows[i]["GPSImeiName"].ToString();
                        GPSImeiInfo.GPSImeiTypeName = ds.Tables[0].Rows[i]["GPSImeiTypeName"].ToString();
                        GPSImeiInfo.GPSModelID = int.Parse(ds.Tables[0].Rows[i]["GPSModelID"].ToString());
                        GPSImeiInfo.GPSModelName = ds.Tables[0].Rows[i]["GPSModelName"].ToString();
                        GPSImeiInfo.GPSTypeID = int.Parse(ds.Tables[0].Rows[i]["GPSTypeID"].ToString());
                        GPSImeiInfo.Image = ds.Tables[0].Rows[i]["Image"].ToString();
                        GPSImeiInfo.ImeiSimNumber = ds.Tables[0].Rows[i]["ImeiSimNumber"].ToString();
                        GPSImeiInfo.LastMessageImei = ds.Tables[0].Rows[i]["LastMessageImei"].ToString();
                        GPSImeiInfo.Latitude = float.Parse(ds.Tables[0].Rows[i]["Latitude"].ToString());
                        GPSImeiInfo.Longitude = float.Parse(ds.Tables[0].Rows[i]["Longitude"].ToString());
                        GPSImeiInfo.Speed = float.Parse(ds.Tables[0].Rows[i]["Speed"].ToString());//float.Parse(ds.Tables[0].Rows[i]["Speed"].ToString());

                        GPSImeiInfoReturn.Add(GPSImeiInfo);
                    }
                }
            }
            catch (Exception ex){
                GPSImeiInfoReturn.Clear();
                GPSImeiInfo = new Entities.GPSImeiInfo();
                GPSImeiInfo.GPSImeiName = ex.Message;
                GPSImeiInfoReturn.Add(GPSImeiInfo);
                
            }

            return GPSImeiInfoReturn;
        }
            
            

        public Entities.Users IdentifiedUser(int companyID, string username, string password)
        {
            Entities.Users UserEntityReturn = new Entities.Users();

            if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
            {
                GPSTrackingDBV2.Entities.Users UserEntity;
                
                //if (companyID != null)
                //{
                //    if (companyID > 0)
                //        username = companyID + "-" + username;                    
                //}
                
                UserEntity = GPSTrackingDBV2.Data.DataRepository.UsersProvider.GetByUsernameCompanyId(username,companyID);

                if (UserEntity != null)
                {
                    if (UserEntity.Password == password)
                    {
                        UserEntityReturn.Username = UserEntity.Username;
                        UserEntityReturn.Name = UserEntity.Name;
                        UserEntityReturn.Active = UserEntity.IsActive;
                        UserEntityReturn.Exists = true;
                        UserEntityReturn.Identified = true;
                    }
                    else
                    {
                        UserEntityReturn.Identified = false;
                        UserEntityReturn.Exists = true;
                    }
                }
                else
                    UserEntityReturn.Exists = false;

            }
            return UserEntityReturn;
        }
    }
}
